<?php

	include_once("../../api/classes/gbif.custom-eml.php");
	include_once("../../api/classes/gbif.general.php");

	$template = trim($_REQUEST['template']);
	$template = ($template == '') ? '../../templates/Merged-Checklist/checklist-1.1.xlsx' : $template;

	$spreadsheet = new General( $template );
	$eml = new CustomEml();

	$sheet = $spreadsheet->getSheet(1);

	$eml->createEmlFromWorksheet( $sheet, true );
	$errors = $eml->validate();
	if (!$errors) {
		$eml->generateXml(0);
		$xml = $eml->getRawXml();
		file_put_contents("eml.xml", $xml );
	}
#	print $xml;
/*
	$xdoc = new DomDocument;
	$xmlfile = 'eml.xml';
#	$xmlschema = 'https://code.ecoinformatics.org/code/eml/tags/RELEASE_EML_2_1_0/eml.xsd';
	$xmlschema = 'http://rs.gbif.org/schema/eml-gbif-profile/dev/eml-gbif-profile.xsd';
	//Load the xml document in the DOMDocument object
	$xdoc->Load($xmlfile);
	//Validate the XML file against the schema

function myErrorHandler($errno, $errstr, $errfile, $errline) {
#	print $errno . "<br>";
	print $errstr . "<br>";
#	print $errfile . "<br>";
#	print $errline . "<br>";
#	print "<hr>";

    return true;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

#	$old_track = ini_set('track_errors', '1');

	$res = @$xdoc->schemaValidate($xmlschema);
#	print	$php_errormsg;
	print "<pre>";
	print_r($php_errormsg);
#	ini_set('track_errors', $old_track);
	exit();
	if ($res) {
		print "$xmlfile is valid.\n";
	} else {
		print "$xmlfile is invalid.\n";
	}
*/
?>
<html><style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
}
body {
	margin-left: 10px;
	margin-top: 10px;
}
-->
</style><title>EML Generator</title>
	<body link="#009900" vlink="#009900" alink="#009900">
		
		<h2>EML Test Generator</h2>
<?php
	if ($errors) {
		print "<pre>";
		print_r( $eml->getIssues() );		
	} else {
?>
		<p>View sample eml file: <a href="eml.xml">eml.xml</a></p>
<?php		
	}
?>
	</body>
</html>